<?php
/*
 *  Maniter: A webbased business managment tool.
 *  Copyright (C) 2007  Dimitri Michaux <dimitri.michaux@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * edit.php, Maniter
 * 
 * Edit and create purchaseorders/offers
 *
 * @author Dimitri Michaux
 * @version 0.1
 * @package maniter_core
 */

require_once( "lib-common.php" );

if( isset($_GET['mode']) && $_GET['mode'] == "new" )
{
  if( isset($_GET['type']) && $_GET['type']=='order' )
  {
    $file = new PurchaseOrder;
    $file->newPurchase();
    PurchaseOrder::save($file);
    $tpl->editPage( $file );
  }
  elseif( isset($_GET['type']) && $_GET['type']=='offer' )
  {
    $file = new PurchaseOffer();
    $file->newPurchase();
    PurchaseOffer::save($file);
    $tpl->editPage( $file );
  }
  
  header( 'Location: edit.php?mode=edit&id=' . $file->getId() );
  exit;
}
elseif( (isset($_GET['mode']) && $_GET['mode'] == "edit") && (isset($_GET['id']) && strlen($_GET['id'])==32) ) {

	if( substr($_GET['id'], 0, 2)=='or' )
		$file =& PurchaseOrder::getPurchaseOrder( $_GET['id'] );
	elseif( substr($_GET['id'], 0, 2)=='of' )
		$file =& PurchaseOffer::getPurchaseOffer( $_GET['id'] );
	else{
		$tpl->addTab( "Boodschap", "Niets om te doen." );
		$tpl->output();
		exit;
	}
	
	if( is_array($_POST['description']) && is_array($_POST['amount']) && is_array($_POST['price']) && is_array($_POST['vat']) ) {
		for( $i=0 ; $i<$file->purchases() ; $i++ ){
			$file->getPurchase($i)->setAmount($_POST['amount'][$i])->setDescription($_POST['description'][$i])->setPrice($_POST['price'][$i])->setVat($_POST['vat'][$i]);
		}
	}
		
	//save date
	if( isset($_POST['month']) && isset($_POST['day']) && isset($_POST['year']) )
		$file->getTimestamp()->setTime( $_POST['month'], $_POST['day'], $_POST['year'] );
	
	//save client information
	if( isset($_POST['company']) )
		$file->getClientInfo()->setCompany( $_POST['company'] );
		
	if( isset($_POST['name']) )
		$file->getClientInfo()->setName( $_POST['name'] );
	
	if( isset($_POST['street']) )
		$file->getClientInfo()->setStreet( $_POST['street'] );
	
	if( isset($_POST['city']) )
		$file->getClientInfo()->setCity( $_POST['city'] );
	
	if( isset($_POST['phone']) )
		$file->getClientInfo()->setPhone( $_POST['phone'] );
	
	if( isset($_POST['fax']) )
		$file->getClientInfo()->setFax( $_POST['fax'] );
	
	if( isset($_POST['vatnr']) )
		$file->getClientInfo()->setVatNr( $_POST['vatnr'] );
	
	if( isset($_POST['action']) ){
		switch( $_POST['action'] ){
			case "addpurchase":
				$file->newPurchase();
				break;
			
			case "removepurchase":
				$file->removePurchase( $_POST['purchaseid'] );
				break;
				
			default:
				//do nothing, just save
				break;
		}
	}
	
	if( isset($_POST['advance']) && get_class($file)=='PurchaseOrder' )
		$file->setAdvance( $_POST['advance'] );
	
	if( isset($_POST['footertext']) )
		$file->setFooterText( $_POST['footertext'] );
		
	if( isset($_POST['headertext']) && get_class($file)=='PurchaseOffer' )
		$file->setHeaderText( $_POST['headertext'] );
	
	//if the finfish button is pushed the purchase order should be set finished
	//and the user should be redirected to the main page
	if( isset($_POST['save']) ){
          $file->setFinished();
          
          if( get_class($file) == 'PurchaseOrder' )
            PurchaseOrder::save( $file );
          elseif( get_class($file) == 'PurchaseOffer' )
            PurchaseOffer::save( $file );
          
          $tpl->redirect( "index.php" );
          exit;
	}
	
	$file->unSetFinished();
	
  if( get_class($file) == 'PurchaseOrder' )
    PurchaseOrder::save( $file );
  elseif( get_class($file) == 'PurchaseOffer' )
    PurchaseOffer::save( $file );
	
	$tpl->editPage( $file );
}else
	$tpl->addTab( "Boodschap", "Niets om te doen." );

if( get_class($file)=='PurchaseOrder' )
	$tpl->setTitle( "Factuur opstellen" );
elseif( get_class($file)=='PurchaseOffer' )
	$tpl->setTitle( "Offerte opstellen" );
	
$tpl->addJavaScript('java/edit.js');
$tpl->output();
?>
